package TUILayer;

import ModelLayer.*;
import ControlLayer.*;
import java.util.*;
/**
 * Write a description of class MenuLoans here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuLoans
{
    Scanner keyboard = new Scanner(System.in);
    public MenuLoans()
    {
        
    }
    
    public void loansMenu()
    {
        boolean back = false;
        while(!back)
        {
            int choiseLoans = writeLoans();
            if(choiseLoans == 1)
            {
                startLoans();
            }
            else
            {
                if(choiseLoans == 2)
                {
                    endLoans();
                }
                else
                {
                    if(choiseLoans == 3)
                    {
                        showLoans();
                    }
                    else
                    {
                        if(choiseLoans == 4)
                        {
                            back = true;
                        }
                        else
                        {
                            System.out.println("Command not valid.");
                        }
                    }
                }
            }
        }
    }
    
    private int writeLoans()
    {
        System.out.println(" ");
        System.out.println("============================");
        System.out.println("|       LOANS SELECTION    |");
        System.out.println("============================");
        System.out.println("| Options:                 |");
        System.out.println("|        1. Start Loans    |");
        System.out.println("|        2. End Loans      |");
        System.out.println("|        3. Show Loans     |");
        System.out.println("|        4. Back           |");
        System.out.println("============================");
        System.out.println("     Make your choise: "     );
        
        int choiseLoans = keyboard.nextInt();
        
        return choiseLoans;
    }
    
    private void startLoans()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            LoanControls loanControl = new LoanControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input loan ID(6 symbols): ");
                String loanID = keyboard.nextLine();
                if(loanID.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Loan ID MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    System.out.println("Input borrow date(format: dd.MM.yyyy): ");
                    String borrowDate = keyboard.nextLine();
                    
                    DatesControls datesControl = new DatesControls();
                    datesControl.isValid(borrowDate);
                    
                    if(datesControl.resutValid().equals("Date valid."))
                    {
                        System.out.println("Input friend's ID(6 symbols): ");
                        String personID = keyboard.nextLine();
                        if(personID.length() != 6)
                        {
                            System.out.println(" ");
                            System.out.println("Person ID MUST BE 6 symbols long!");
                            end = true;
                        }
                        else
                        {
                             System.out.println("Input copy's serial number(6 symbols): ");
                             String serialNumber = keyboard.nextLine();
                             if(serialNumber.length() != 6)
                             {
                                 System.out.println(" ");
                                 System.out.println("Serial number MUST BE 6 symbols long!");
                                 end = true;
                             }
                             else
                             {
                                 loanControl.startLoans(loanID, borrowDate, personID, serialNumber);
                                 System.out.println(" ");
                                 System.out.println(loanControl.startLoanResult());
                                 end = true;
                             }
                        }
                    }
                    else
                    {
                        System.out.println(" ");
                        System.out.println("Borrow date NOT VALID!");
                        end = true;
                    }
                }
            }
        }
    }
                    
    
    private void endLoans()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            LoanControls loanControl = new LoanControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input loan ID(6 symbols): ");
                String loanID = keyboard.nextLine();
                if(loanID.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Loan ID MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    System.out.println("Input current date(format: dd.MM.yyyy): ");
                    String currentDate = keyboard.nextLine();
        
                    DatesControls datesControl = new DatesControls();
                    datesControl.isValid(currentDate);
        
                    if(datesControl.resutValid().equals("Date valid."))
                    {
                        loanControl.endLoans(loanID, currentDate);
                        System.out.println(" ");
                        System.out.println(loanControl.finishLoanResult());
                        end = true;
                    }
                    else
                    {
                        System.out.println(" ");
                        System.out.println("Current date NOT VALID!");
                        end = true;
                    }
                }
            }
        }
    }
    
    private void showLoans()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            LoanControls loanControl = new LoanControls();
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input loan ID( 6 symbols): ");
                String loanID = keyboard.nextLine();
                if(loanID.length() != 6)
                {
                    System.out.println(" ");
                    System.out.println("Loan ID MUST BE 6 symbols long!");
                    end = true;
                }
                else
                {
                    loanControl.showLoans(loanID);
                    System.out.println(" ");
                    System.out.println(loanControl.showLoans(loanID));
                    end = true;
                }
            }
        }
    }
}
